package com.example.nomemo;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity implements Runnable, AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
    Handler handler;
    private static final String TAG = "MainActivity";
    private ArrayList<HashMap<String, String>> listItems;//存放文字、图片信息
    private SimpleAdapter listItemAdatper;
    private String logDate = "";
    private final String DATE_SP_KEY = "lastRateDateStr";
    private ListView listView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_main);
        SharedPreferences sp = getSharedPreferences("time", Context.MODE_PRIVATE);
        logDate = sp.getString(DATE_SP_KEY, "");

        Log.i("List", "lastRateDateStr=" + logDate);

        initListView();
        listView=(ListView) findViewById(R.id.listview);
        listView.setAdapter(listItemAdatper);
        Thread t = new Thread(this);
        t.start();
        handler = new Handler(Looper.myLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                if (msg.what == 6) {
                    listItems = (ArrayList<HashMap<String, String>>) msg.obj;//list包要导入正确
                    listItemAdatper = new SimpleAdapter(MainActivity.this, listItems,
                            R.layout.activity_main,//xml布局实现
                            new String[]{"ItemTitle", "ItemDetail"},
                            new int[]{R.id.itemTitle, R.id.itemDetail}
                    );
                    listView.setAdapter(listItemAdatper);
                }
                super.handleMessage(msg);
            }
        };
        listView.setOnItemClickListener(this);
        listView.setOnItemLongClickListener(this);
        listView.setEmptyView(findViewById(R.id.nodata));
    }


    private void initListView() {
        listItems = new ArrayList<HashMap<String, String>>();
        for (int i = 0; i < 10; i++) {
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", "rate: " + i);//标题文字
            map.put("ItemDetail", "detail" + i);//详情描述
            listItems.add(map);
        }
        //生成适配器 的item和动态数组对应的元素
        listItemAdatper = new SimpleAdapter(this, listItems,
                R.layout.activity_main,//xml布局实现
                new String[]{"ItemTitle", "ItemDetail"},
                new int[]{R.id.itemTitle, R.id.itemDetail}
        );
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.notice, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.menu_set) {
            Intent config = new Intent(this, NewsActivity.class);
            startActivity(config);
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }

    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
        Log.i(TAG, "onItemLongClick: 长按操作");
        AlertDialog.Builder bulider = new AlertDialog.Builder(this);
        bulider.setTitle("提示")
                .setMessage("是否删除该备忘？")
                .setPositiveButton("是", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        listItems.remove(position);
                        Log.i(TAG, "onClick: position"+position);
                        listItemAdatper.notifyDataSetChanged();
                        NoticeManager manager = new NoticeManager(MainActivity.this);
                        manager.deleteAll();
                        for(int i=0;i<listItems.size();i++){
                            String Str1, Str2;
                            HashMap<String, String> str2 = listItems.get(i);
                            Str1 = str2.get("ItemTitle");
                            Str2 = str2.get("ItemDetail");
                            manager.add(new NoticeItem(Str1, Str2));
                        }
                        List<NoticeItem> all = manager.listAll();
                        for(NoticeItem i:all){
                            Log.i(TAG, "onCreate: 取出数据：title="+i.getNotTitle());
                            Log.i(TAG, "onCreate: 取出数据：time="+i.getNotTime());
                        }
                    }
                }).setNegativeButton("否", null);
        bulider.create().show();
        return true;
    }

    @Override
    public void run() {
        //获取数据库数据
        List<HashMap<String, String>> retList = new ArrayList<HashMap<String, String>>();
        NoticeManager manager = new NoticeManager(MainActivity.this);
        List<NoticeItem> all = manager.listAll();
        for(NoticeItem i:all){
            HashMap<String, String> map = new HashMap<String, String>();
            map.put("ItemTitle", i.getNotTitle());
            map.put("ItemDetail", i.getNotTime());
            retList.add(map);
            Log.i(TAG, "onCreate: 取出数据：id="+i.getId());
            Log.i(TAG, "onCreate: 取出数据：title="+i.getNotTitle());
            Log.i(TAG, "onCreate: 取出数据：time="+i.getNotTime());
        }
        Message msg = handler.obtainMessage(6);
        msg.obj = retList;
        handler.sendMessage(msg);
    }
}